//
//  GKStatus.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/13.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit

class GKStatus: NSObject {

    /// 微博创建时间
    var created_at: String?
    
    /// 微博ID
    var id:	Int64 = 0
    
    /// 微博信息内容
    var text: String?
    
    /// 微博来源
    var source: String?
    
    /// 被转发的原微博信息字段，当该微博为转发微博时返回
    var retweeted_status: GKStatus?
    
    /// 微博的配图 [Int], [String: AnyObject], [[String: AnyObject]]
    /// 数组,里面保存的是字典
    var pic_urls:[[String: AnyObject]]?{
        
        didSet
        {
            let count = pic_urls?.count ?? 0
            if count == 0
            {
                return
            }
            
//            GKPrint(pic_urls)
            
            // 创建storePicutreURLs数组保存NSURL
            storePicURLs = [NSURL]()
            for dict in pic_urls!
            {
                // 获取到每个字典,解析thumbnail_pic所对应的value,value是一个图片的地址(String)
                let urlString = dict["thumbnail_pic"] as! String
                let url = NSURL(string: urlString)
                storePicURLs?.append(url!)
            }
            
        }
        
        
    }
    
    var storePicURLs : [NSURL]?
    
    //服务器返回的是字典,需要拦截字典转成模型
    var user: GKUser?
    
    //字典转模型
    init(dict:[ String : AnyObject ])
    {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    
    //setValuesForKeysWithDictionary 这个方法会来频繁调用下面的方法 字典中每一个键值对会调用一次
    override func setValue(value: AnyObject?, forKey key: String)
    {
//        GKPrint("key\(key):value\(value)")
        //拦截key名为user的value ,是一个包含一条完整微博所有数据的dict
        if key == "user"
        {
            if let dict = value as? [String : AnyObject]
            {
                //字典转模型
                user = GKUser(dict: dict)
            }
            return
        }
        if key == "retweeted_status" , let dict = value as? [String : AnyObject]
        {
            retweeted_status = GKStatus(dict: dict)
            return//一定要记得return ,否则调用下面的 父类的 Setvalue forkey 就失效了
        }
        
        
        super.setValue(value, forKey: key)
    }
    
    //模型的打印属性 ,重写它的description
    override var description: String
    {
        get
        {
            let keys = [ "created_at", "id", "text", "source", "pic_urls","user","retweeted_status" ]
            return "\n\t微博模型:\(self.dictionaryWithValuesForKeys(keys).description)"
        }
    }
    
    
 
    
    
}
